#include<iostream>
#include<vector>

using namespace std;

int main()
{
	string s;
	int k;
	cin >> s >> k;

	int n = s.size();
	int res = 0;
	for (int i = 1; i <= 26; i++)
	{
		int l = 0, r = 0;
		vector<int> st(26);
		int tot = 0, valid = 0;
		while (r < n)
		{
			auto t = s[r] - 'a';
			st[t]++;
			if (st[t] == 1) tot++;
			if (st[t] == k)	valid++;
			r++;

			while (tot > i)
			{
				auto c = s[l] - 'a';
				st[c]--;
				if (st[c] == 0)	tot--;
				if (st[c] == k-1)	valid--;
				l++;
			}

			if (tot == i && valid == i)
				res = max(res, r - l);
		}
	}
	cout << res << endl;
	return 0;
}